﻿using UnityEditor;
using UnityEngine;
using System.IO;
using System;
using System.Text;

namespace IFramework
{

    public partial class FormatUIScriptProcessor : UnityEditor.AssetModificationProcessor
    {

        /// <summary>
        /// 此函数在asset被创建完，文件已经生成到磁盘上，但是没有生成.meta文件和import之前被调用
        /// </summary>
        /// <param name="newFileMeta">newfilemeta 是由创建文件的path加上.meta组成的</param>
        public static void OnWillCreateAsset(string newFileMeta)
        {
            string newFilePath = newFileMeta.Replace(".meta", "");
            string fileExt = Path.GetExtension(newFilePath);
            if (fileExt != ".cs") return;
            //注意，Application.datapath会根据使用平台不同而不同
            string realPath = Application.dataPath.Replace("Assets", "") + newFilePath;
            string txt = File.ReadAllText(realPath);
            //这里实现自定义的一些规则
            txt = txt.Replace("#SCRIPTNAME#", Path.GetFileNameWithoutExtension(newFilePath));
            txt = txt.Replace("#AUTHOR#", PlayerSettings.companyName);
            txt = txt.Replace("#VERSION#", "1.0");
            txt = txt.Replace("#UNITYVERSION#", Application.unityVersion);
            txt = txt.Replace("#DATE#",DateTime.Now.ToString("yyyy-MM-dd"));

            File.WriteAllText(realPath, txt,Encoding.UTF8);
        }

    }
}